package com.easylinkin.linkappapi.lobar.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.lobar.entity.WarningNotifier;
import com.easylinkin.linkappapi.lobar.mapper.WarningNotifierMapper;
import com.easylinkin.linkappapi.lobar.service.WarningNotifierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 预警通知人表 服务实现类
 * </p>
 *
 * @author Xiaoyang
 * @since 2022-10-24
 */
@Service
public class WarningNotifierServiceImpl extends ServiceImpl<WarningNotifierMapper, WarningNotifier> implements WarningNotifierService {

  @Override
  public void addOrUpdateWarningNotifier(List<Integer> warningTypes, List<String> notifiers,
      Integer noticeSwitch, String tenantId) {
    // 查询已有配置并删除
    QueryWrapper<WarningNotifier> warningNotifierQueryWrapper = new QueryWrapper<>();
    warningNotifierQueryWrapper.eq("tenant_id_",tenantId);
    List<WarningNotifier> selectOldList = baseMapper.selectList(warningNotifierQueryWrapper);
    if(CollectionUtil.isNotEmpty(selectOldList)){
      baseMapper.delete(warningNotifierQueryWrapper);
    }
    // 新增通知配置
    if(CollectionUtil.isNotEmpty(warningTypes)&&CollectionUtil.isNotEmpty(notifiers)){
      String warningNotifiers = String.join(",",notifiers);
      List<WarningNotifier> warningNotifierList = new ArrayList<>();
      warningTypes.forEach(p->{
        WarningNotifier warningNotifier = new WarningNotifier();
        warningNotifier.setTenantId(tenantId);
        warningNotifier.setNoticeSwitch(
            noticeSwitch == null ? 0 : noticeSwitch);
        warningNotifier.setWarningType(p);
        warningNotifier.setWarningNotifiers(warningNotifiers);
        warningNotifierList.add(warningNotifier);
      });
      if(CollectionUtil.isNotEmpty(warningNotifierList)){
        warningNotifierList.forEach(p->baseMapper.insert(p));
      }
    }

  }

  @Override
  public List<String> findNotifiersByWarningTypesAndTenantId(Integer warningType,
      String tenantId) {
    QueryWrapper<WarningNotifier> warningNotifierQueryWrapper = new QueryWrapper<>();
    warningNotifierQueryWrapper.eq("tenant_id_",tenantId)
        .eq("notice_switch",1)
    .eq("warning_type",warningType);
    WarningNotifier warningNotifier = baseMapper.selectOne(warningNotifierQueryWrapper);
    if(null != warningNotifier){
      List<String> userIds = Arrays.asList(warningNotifier.getWarningNotifiers().split(","));
      return userIds;
    }
    return null;
  }

  @Override
  public List<WarningNotifier> findAllNotifiersByTenantId(String tenantId) {
    QueryWrapper<WarningNotifier> warningNotifierQueryWrapper = new QueryWrapper<>();
    warningNotifierQueryWrapper.eq("tenant_id_",tenantId);
    List<WarningNotifier> warningNotifierList = baseMapper.selectList(warningNotifierQueryWrapper);
    return warningNotifierList;
  }
}
